package com.ddpt.platform.common.upload.provider;

import cn.hutool.core.io.IoUtil;
import com.ddpt.platform.common.exception.CommonException;
import com.ddpt.platform.common.upload.FileUploadBean;
import com.ddpt.platform.common.util.AssertUtil;
import io.minio.MinioClient;
import io.minio.ObjectWriteResponse;
import io.minio.PutObjectArgs;
import org.apache.commons.lang3.StringUtils;

public class MinioUploadProvider {

    private final MinioConfig config;

    public MinioUploadProvider(MinioConfig config) {
        this.config = config;
        AssertUtil.isNull(config,"配置信息为空");
    }

    public FileUploadBean upload(String fileCanonicalName,String contentType, byte[] buts){
        //文件大小
        int available = buts.length;
        //后缀名
        String suffix = null;
        if(fileCanonicalName.contains(".")){
            suffix = fileCanonicalName.substring(fileCanonicalName.lastIndexOf(".")+1);
        }

        //确定contentType的取值
        if(StringUtils.isBlank(contentType)){
            contentType = chooseContentType(suffix);
        }

        PutObjectArgs putObjectArgs = PutObjectArgs.builder()
                .bucket(config.getMinioBucketName())
                .object(fileCanonicalName)
                .contentType(contentType)
                .stream(IoUtil.toStream(buts), available, -1)
                .build();
        ObjectWriteResponse res = null;
        try {
            res = MinioClient.builder()
                    .endpoint(config.getMinioEndpoint())
                    .credentials(config.getMinioAccessKey(), config.getMinioSecretKey())
                    .build().putObject(putObjectArgs);
        } catch (Exception e) {
            throw new CommonException("文件上传失败");
        }
        String url = config.getMinioVisitHost()+"/"+config.getMinioBucketName()+"/"+res.object();

        FileUploadBean result = new FileUploadBean();
        result.setUrl(url);
        result.setSize(available);
        result.setFileName(fileCanonicalName);
        result.setSuffix(suffix);
        return result;
    }

    private String chooseContentType(String suffix){
        if(StringUtils.isBlank(suffix)){
            return "application/octet-stream;";
        }
        if(suffix.equalsIgnoreCase("gif")){
            return "image/gif;";
        }
        if(suffix.equalsIgnoreCase("jpg")){
            return "image/jpeg;";
        }
        if(suffix.equalsIgnoreCase("jpeg")){
            return "image/jpeg;";
        }
        if(suffix.equalsIgnoreCase("png")){
            return "image/png;";
        }
        if(suffix.equalsIgnoreCase("pdf")){
            return "application/pdf;";
        }
        if(suffix.equalsIgnoreCase("doc")){
            return "application/msword;";
        }
        if(suffix.equalsIgnoreCase("docx")){
            return "application/vnd.openxmlformats-officedocument.wordprocessingml.document;";
        }
        if(suffix.equalsIgnoreCase("xls")){
            return "application/vnd.ms-excel;";
        }
        if(suffix.equalsIgnoreCase("xlsx")){
            return "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;";
        }
        if(suffix.equalsIgnoreCase("ppt")){
            return "application/vnd.ms-powerpoint;";
        }
        if(suffix.equalsIgnoreCase("pptx")){
            return "application/vnd.openxmlformats-officedocument.presentationml.template;";
        }
        return "application/octet-stream;";
    }
}
